# ========================================
# 校园失物招领系统 - 快速启动脚本
# ========================================
# 使用方法：
#   1. 修改下面的数据库密码（如果不是 123456）
#   2. 在 PowerShell 中执行: .\启动环境变量.ps1
#   3. 然后在 IDEA/Eclipse 中启动 LostAndFoundApplication
# ========================================

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  校园失物招领系统 - 环境变量配置" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# ==================== 必需环境变量 ====================

# AES 加密主密钥（必须是 16 字节）
$env:AES_ENCRYPTION_KEY = "campus-aes-key!"

# 数据库连接 URL
$env:DB_URL = "jdbc:mysql://localhost:3306/campus_lost_found?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"

# 数据库用户名
$env:DB_USERNAME = "root"

# 数据库密码（⚠️ 请修改为你自己的数据库密码）
$env:DB_PASSWORD = "123456"  # ← 如果你的MySQL密码不是 123456，请修改这里

# ==================== 可选环境变量 ====================

# JWT 签名密钥
$env:JWT_SECRET_KEY = "campus-lost-found-secret-key-2024"

# Redis 配置（已有默认值，通常不需要设置）
$env:REDIS_HOST = "localhost"
$env:REDIS_PORT = "6379"
$env:REDIS_PASSWORD = ""

# ==================== 显示配置信息 ====================

Write-Host "✓ 环境变量已设置成功！" -ForegroundColor Green
Write-Host ""
Write-Host "当前配置:" -ForegroundColor Yellow
Write-Host "  AES密钥: $env:AES_ENCRYPTION_KEY" -ForegroundColor White
Write-Host "  数据库: localhost:3306/campus_lost_found" -ForegroundColor White
Write-Host "  用户名: $env:DB_USERNAME" -ForegroundColor White
Write-Host "  密码: $(if ($env:DB_PASSWORD) { '******' } else { '未设置' })" -ForegroundColor White
Write-Host "  Redis: $env:REDIS_HOST`:$env:REDIS_PORT" -ForegroundColor White
Write-Host ""

# ==================== 验证必需变量 ====================

$allSet = $true

if (-not $env:AES_ENCRYPTION_KEY) {
    Write-Host "✗ 缺少 AES_ENCRYPTION_KEY" -ForegroundColor Red
    $allSet = $false
}

if (-not $env:DB_URL) {
    Write-Host "✗ 缺少 DB_URL" -ForegroundColor Red
    $allSet = $false
}

if (-not $env:DB_PASSWORD) {
    Write-Host "✗ 缺少 DB_PASSWORD" -ForegroundColor Red
    $allSet = $false
}

if ($allSet) {
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host "✓ 所有必需的环境变量都已设置" -ForegroundColor Green
    Write-Host "========================================" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "下一步操作:" -ForegroundColor Yellow
    Write-Host "  1. 确保 MySQL 和 Redis 服务已启动" -ForegroundColor White
    Write-Host "  2. 在 IDEA 中点击运行按钮启动应用" -ForegroundColor White
    Write-Host "  3. 或者在命令行执行: mvn spring-boot:run" -ForegroundColor White
    Write-Host ""
    Write-Host "提示: 此 PowerShell 窗口必须保持打开状态" -ForegroundColor Yellow
    Write-Host "      或者在 IDEA 中配置环境变量" -ForegroundColor Yellow
    Write-Host ""
} else {
    Write-Host ""
    Write-Host "⚠️ 请检查并设置缺失的环境变量" -ForegroundColor Red
    Write-Host ""
}

